package com.workmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.ClassJobTaskBean;
import com.workmanager.bean.JobTaskBean;
import com.workmanager.bean.TeacherBean;
import com.workmanager.bean.WorkBean;
import com.workmanager.service.TeacherWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2016/8/29.
 * 用于处理王晶所负责的老师功能
 */
@RequestMapping("/teacherWorkController")
@Controller("teacherWorkController")
public class TeacherWorkController {

    @Autowired
    @Resource(name = "teacherWorkService")
    private TeacherWorkService teacherWorkService;

    //获取到所有的作业任务
    @RequestMapping("/getall")
    public void getall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TeacherBean teacher = (TeacherBean)request.getSession().getAttribute("教师");
        String teacherId = teacher.getTeacher_id();
        List<ClassJobTaskBean> list = teacherWorkService.getAllJobTask(teacherId);
        // List<ClassJobTaskBean> list = teacherWorkService.getAllJobTask("1");
        Map<String,String> para = teacherWorkService.getJobTaskNum(list);
        JSONObject json = new JSONObject();
        json.put("all",list);
        json.put("totalNum",para);
        response.getWriter().print(json.toJSONString());
    }
    //获取到指定搜索的作业任务
    @RequestMapping("/search")
    public void search(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TeacherBean teacher = (TeacherBean)request.getSession().getAttribute("教师");
        String teacherId = teacher.getTeacher_id();
        String text = request.getParameter("text");
        System.out.println("controller----"+text);
        List<ClassJobTaskBean> list = teacherWorkService.getJobTask(teacherId,text);
        // List<ClassJobTaskBean> list = teacherWorkService.getAllJobTask("1");
        Map<String,String> para = teacherWorkService.getJobTaskNum(list);
        JSONObject json = new JSONObject();
        json.put("all",list);
        json.put("totalNum",para);
        response.getWriter().print(json.toJSONString());
    }
    //获取指定一个作业任务编号的作业明细
    @RequestMapping("/getAllwork")
    public void getAllwork(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取到指定作业编号的明细
        String jobtaskId = request.getParameter("jobTaskId");
        List<WorkBean> list = teacherWorkService.getAllwork(jobtaskId);
        JSONObject json = new JSONObject();
        json.put("all",list);
        response.getWriter().print(json.toJSONString());
    }

    //文件下载
    @RequestMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String workId = request.getParameter("workId");
        String filename = teacherWorkService.getFileName(workId);
        String path = request.getServletContext().getRealPath(filename);
        OutputStream out = response.getOutputStream();
        boolean result = teacherWorkService.download(out,path,filename,request,response);
    }
    //作业分享
    @RequestMapping("/share")
    public void share(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String studentId = request.getParameter("studentId");
        String workId = request.getParameter("workId");
        String isGood = request.getParameter("good"); //是否为优秀作业：是：分享 否：取消分享
        JSONObject json = new JSONObject();
        boolean result = teacherWorkService.setGoodJob(studentId,workId,isGood);
        if(result){
            json.put("msg","true");
            json.put("id",workId);
        } else {
            json.put("msg","flase");
            json.put("id",workId);
        }
        response.getWriter().print(json.toJSONString());
    }
    //作业预览
    @RequestMapping("/preview")
    public void preview(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String workId = request.getParameter("workId");
        String jobTaskId = request.getParameter("jobTaskId");
        String url = "/teacher/evaluateWork.jsp";
        WorkBean work = teacherWorkService.getStudentWork(workId);
        request.setAttribute("work",work);
        request.setAttribute("jobTaskId",jobTaskId);
        request.getRequestDispatcher(url).forward(request,response);
    }
    //作业评价
    @RequestMapping("/evaluate")
    public void evaluate(HttpServletRequest request, HttpServletResponse response) throws IOException {

         String workId = request.getParameter("workId");   //对改id的作业进行评价
         String jobTaskId = request.getParameter("jobTaskId");
         String text = request.getParameter("evatext");    //评价内容
        boolean result = teacherWorkService.doEvaluate(workId,text);
        JSONObject json = new JSONObject();
        if(result){
            json.put("msg","true");
        } else {
            json.put("msg","false");
        }
        response.getWriter().print(json.toJSONString());
    }

    /**
     * 处理workDetail.jsp页面需要显示的数据
     */
    @RequestMapping("/workDetail")
    public void workDetail(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String jobTaskId = request.getParameter("id");
        String url = "/teacher/workDetail.jsp";
        ClassJobTaskBean work = teacherWorkService.getAbyId(jobTaskId);
        int totalUnReport = teacherWorkService.getTotalUnreport(jobTaskId);
        request.setAttribute("work",work);
        request.setAttribute("totalUnReport",totalUnReport);
        request.setAttribute("jobTaskId",jobTaskId);
        request.getRequestDispatcher(url).forward(request,response);
    }
    public TeacherWorkService getTeacherWorkService() {
        return teacherWorkService;
    }

    public void setTeacherWorkService(TeacherWorkService teacherWorkService) {
        this.teacherWorkService = teacherWorkService;
    }
}
